// pages/address/address.js
const area = require("../../utils/area.js").default;
import {
  addAddress,
  editAddress
} from '../../network/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEdit: false,
    show: false,
    checked: true,
    areaList: area,
    id: '',
    areaCode: '',
    province: '', //省
    city: '', //市
    country: '', //区
    name: '',
    phone: '',
    detailed: '',
    check: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', (data)=> {
      this.init(data.editAdress)
    })
  },
  //如果是编辑，就初始化数据
  init(data) {
    this.setData({
      isEdit: true,
      checked: data.default === "1" ? true : false,
      areaCode: data.areaCode,
      province: data.province, //省
      city: data.city, //市
      country: data.country, //区
      name: data.name,
      phone: data.phone,
      detailed: data.detailed,
      id: data.id
    })
  },

  //保存，如果是订单过来的就直接使用
  async saveAddress() {
    const {
      isEdit,
      name,
      phone,
      areaCode,
      province,
      city,
      country,
      detailed,
      checked
    } = this.data;
    if(!name || !phone || !areaCode || !province || !city || !country || !country || !detailed){
      wx.showToast({
        title: "请填写完整信息",
        icon: 'none'
      })
      return;
    }
    const params = {
      name,
      phone,
      areaCode,
      province,
      city,
      country,
      detailed,
      default: checked ? "1" : "2"
    }
    console.log(params)
    wx.showLoading({
      title: '保存中',
      mask: true,
    });
    if (isEdit) {
      params.id = this.data.id;
      await editAddress(params).then((result) => {
        wx.hideLoading();
        wx.navigateBack();
      }).catch((error) => {
        wx.hideLoading();
        wx.showToast({
          title: error.message,
          icon: 'none'
        })
      });
    } else {
      
      await addAddress(params).then((result) => {
        wx.hideLoading();
        wx.navigateBack();
      }).catch((error) => {
        wx.hideLoading();
        wx.showToast({
          title: error.message,
          icon: 'none'
        })
      });
    }
  },

  openPopup() {
    this.setData({
      show: true
    })
  },

  //选择省市区
  confirm(options) {
    let values = options.detail.values;
    console.log(options)
    let province = values[0].name;
    let city = values[1].name;
    let country = values[2].name;
    this.setData({
      // show: false,
      province,
      city,
      country,
      areaCode: values[2].code
    })
    this.onClose();
  },

  //取消选择
  cancel() {
    this.onClose();
  },
  //关闭省市区选择
  onClose() {
    this.setData({
      show: false
    })
  },
  // 默认地址开关
  onChange({
    detail
  }) {
    this.setData({
      checked: detail
    });
  },

  //双向数据绑定
  bindName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  bindDetailed(e) {
    this.setData({
      detailed: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})